# this class is currently only in master therefor include source code here
# inherit cargo-update-recipe-crates

#
# Copyright OpenEmbedded Contributors
#
# SPDX-License-Identifier: MIT
#

##
## Purpose:
## This class is used to update the list of crates in SRC_URI
## by reading Cargo.lock in the source tree.
##
## See meta/recipes-devtools/python/python3-bcrypt_*.bb for an example
##
## To perform the update: bitbake -c update_crates recipe-name

addtask do_update_crates after do_patch
do_update_crates[depends] = "python3-native:do_populate_sysroot"

# The directory where to search for Cargo.lock files
CARGO_LOCK_SRC_DIR ??= "${S}"

do_update_crates() {
    nativepython3 - <<EOF

def get_crates(f):
    import tomllib
    c_list = '# from %s' % os.path.relpath(f, '${CARGO_LOCK_SRC_DIR}')
    c_list += '\nSRC_URI += " \\\'
    crates = tomllib.load(open(f, 'rb'))
    for c in crates['package']:
        if 'source' in c and 'crates.io' in c['source']:
            c_list += '\n    crate://crates.io/%s/%s \\\' % (c['name'], c['version'])
    c_list += '\n"\n'
    return c_list

import os
crates = "# Autogenerated with 'bitbake -c update_crates ${PN}'\n\n"
for root, dirs, files in os.walk('${CARGO_LOCK_SRC_DIR}'):
    for file in files:
        if file == 'Cargo.lock':
            crates += get_crates(os.path.join(root, file))
open(os.path.join('${THISDIR}', '${BPN}'+"-crates.inc"), 'w').write(crates)

EOF
}

#####

# If this is git based prefer versioned ones if they exist
# DEFAULT_PREFERENCE = "-1"

# how to get firecracker could be as easy as but default to a git checkout:
# SRC_URI += "crate://crates.io/firecracker/0.24.3"
SRC_URI = "git://github.com/firecracker-microvm/firecracker.git;protocol=https;nobranch=1;branch=main \
           file://0001-fdt-lib.patch \
           file://0003-Remove-abort-panic-from-the-workspace.patch \
           "
SRCREV = "408f2fca1a3a55027245513531bd6abca8e39017"
S = "${WORKDIR}/git"
CARGO_SRC_DIR = "src/firecracker"

LIC_FILES_CHKSUM = " \
    file://LICENSE;md5=3b83ef96387f14655fc854ddc3c6bd57 \
"

SUMMARY = "Firecracker enables you to deploy workloads in lightweight virtual machines, called microVMs, which provide enhanced security and workload isolation over traditional VMs, while enabling the speed and resource efficiency of containers."
HOMEPAGE = "https://firecracker-microvm.github.io/"
LICENSE = "Apache-2.0"

DEPENDS += "dtc"

CVE_PRODUCT = "firecracker"

do_compile:prepend() {
    # artifically trigger a re-run of the build script
    touch ${S}/build.rs
}
